<template>
  <div class="p-2">

    <div
      class="mb-[10px]"
      v-show="showSearch"
    >
      <el-card shadow="hover">
        <el-form
          ref="queryFormRef"
          :model="queryParams"
          :inline="true"
          label-width="auto"
        >
          <el-form-item
            label="创建时间"
          >
            <el-date-picker
              v-model="dateRange"
              value-format="YYYY-MM-DD"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width: 240px"
            ></el-date-picker>
          </el-form-item>
          <el-form-item
            label="业务员名称"
            prop="name"
          >
            <el-input
              v-model="queryParams.name"
              placeholder="请输入业务员名称"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item
            label="业务员编号"
            prop="no"
          >
            <el-input
              v-model="queryParams.no"
              placeholder="请输入业务员编号"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item
            label="代理商编号"
            prop="channelNo"
          >
            <el-input
              v-model="queryParams.channelNo"
              placeholder="请输入代理商编号"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item
            label="代理商名称"
            prop="channelName"
          >
            <el-input
              v-model="queryParams.channelName"
              placeholder="请输入代理商名称"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              @click="handleQuery"
              icon="Search"
              v-hasPermi="['payment:channelSalesman:list']"
            >查询</el-button>
            <el-button
              @click="resetQuery"
              icon="Refresh"
            >重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <el-card shadow="hover">
      <template #header>
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button
              type="primary"
              @click="handleAdd()"
              v-hasPermi="['payment:channelSalesman:add']"
              icon="Plus"
            >新增</el-button>
          </el-col>
          <right-toolbar
            v-model:showSearch="showSearch"
            @queryTable="getList"
            :search="true"
          ></right-toolbar>
        </el-row>
      </template>

      <el-table
        v-loading="loading"
        :data="salesmanList"
        border
      >
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
        />
        <el-table-column
          label="业务员名称"
          align="center"
          prop="name"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="业务员编号"
          align="center"
          prop="no"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="代理商编号"
          align="center"
          prop="channelNo"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="代理商名称"
          align="center"
          prop="channelName"
        />
        <el-table-column
          label="备注"
          align="center"
          prop="remarker"
        />

        <el-table-column
          label="操作"
          fixed="right"
          align="center"
          width="180"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-tag
              link
              type="primary"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['payment:channelSalesman:edit']"
            >编辑</el-tag>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改业务员配置对话框 -->
    <el-dialog
      ref="formDialogRef"
      :title="dialog.title"
      v-model="dialog.visible"
      width="600px"
      append-to-body
      @close="closeDialog"
    >
      <el-form
        :model="form"
        ref="userFormRef"
        label-width="auto"
        :rules="rules"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="业务员名称"
              prop="name"
              required
            >
              <el-input
                v-model="form.name"
                placeholder="请输入业务员名称"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel()">关 闭</el-button>
          <el-button
            type="primary"
            @click="submitForm"
          >确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Salesman" lang="ts">
import api from '@/api/system/salesman';
import { SalesmanForm, SalesmanQuery, SalesmanVO } from '@/api/system/salesman/types';

const router = useRouter();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const salesmanList = ref<SalesmanVO[]>();
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const dateRange = ref<[DateModelType, DateModelType]>(['', '']);

const queryFormRef = ref<ElFormInstance>();
const userFormRef = ref<ElFormInstance>();
const formDialogRef = ref<ElDialogInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const data = reactive<PageData<SalesmanForm, SalesmanQuery>>({
  form: {
    name: '',
    id: ''
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    channelNo: '',
    channelName: '',
    name: '',
    no: '',
    startTime: '',
    endTime: ''
  },
  rules: {
    userName: [{ required: true, message: '请输入业务员名称', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs<PageData<SalesmanForm, SalesmanQuery>>(data);

/** 查询业务员列表 */
const getList = async () => {
  loading.value = true;
  const res = await api.channelSalesmanList(proxy?.addDateRange(queryParams.value, dateRange.value));
  loading.value = false;
  salesmanList.value = res.rows;
  total.value = res.total;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  dateRange.value = ['', ''];
  queryFormRef.value?.resetFields();
  queryParams.value.pageNum = 1;
  handleQuery();
};

/** 重置操作表单 */
const reset = () => {
  form.value = {
    name: '',
    id: ''
  };
  userFormRef.value?.resetFields();
};
/** 取消按钮 */
const cancel = () => {
  dialog.visible = false;
  reset();
};

/** 新增按钮操作 */
const handleAdd = async () => {
  reset();
  dialog.visible = true;
  dialog.title = '新增业务员';
};
/** 修改按钮操作 */
const handleUpdate = async (row?: any) => {
  reset();
  dialog.visible = true;
  dialog.title = '编辑业务员';
  form.value.name = row.name
  form.value.id = row.id
};

/** 提交按钮 */
const submitForm = () => {
  userFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      let res:any = {}
      if(dialog.title=='新增业务员'){
        res = await api.addSalesman({
          name:form.value.name
        })
      }else if(dialog.title=='编辑业务员'){
        res = await api.updateSalesman(form.value)
      }
      proxy?.$modal.msgSuccess(res.msg);
      dialog.visible = false;
      await getList();
    }
  });
};

/**
 * 关闭业务员弹窗
 */
const closeDialog = () => {
  dialog.visible = false;
  resetForm();
};

/**
 * 重置表单
 */
const resetForm = () => {
  userFormRef.value?.resetFields();
  userFormRef.value?.clearValidate();
};
onMounted(() => {
  getList(); // 初始化列表数据
});
</script>

<style lang="scss" scoped></style>
